package com.lindenvalley.ch.adapters;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.graphics.Color;
import android.text.Spannable;
import android.text.SpannableString;
import android.text.style.AbsoluteSizeSpan;
import android.text.style.ForegroundColorSpan;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.lindenvalley.ch.AbstractHomeActivity;
import com.lindenvalley.ch.R;
import com.lindenvalley.ch.content.Week;
import com.lindenvalley.ch.content.Weeks;
import com.lindenvalley.ch.network.WorkServer;

public class SearchWeekTaskAdapter extends AbstractWeekTaskAdapter implements OnClickListener{

	
	public SearchWeekTaskAdapter(Context context, int textViewResourceId, Weeks weeks){
		super(context, textViewResourceId, weeks);
	}
	
	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		
		if (mWeeks == null)
			return null;		
		
		if (convertView == null){
			convertView = inflater.inflate(mResource, parent, false);
		}
		
		Week week = mWeeks.getReverseWeek(position);
		TextView tv = (TextView) convertView.findViewById(R.id.searchTextView);
		setItemText(tv,week);
		if (!week.isEmpty())
			tv.setCompoundDrawablesWithIntrinsicBounds(mContext.getResources().getDrawable(R.drawable.icon_tab_check_active), null, null, null);
		else
			tv.setCompoundDrawablesWithIntrinsicBounds(mContext.getResources().getDrawable(R.drawable.icon_tab_check_inactive), null, null, null);
		
		ImageView renameImaveView = (ImageView) convertView.findViewById(R.id.searchAusbildRenameView);
		if (renameImaveView != null){
			renameImaveView.setOnClickListener(this);
			renameImaveView.setTag(Long.valueOf(week.getStartDate()));
		}
		
		return convertView;
	}

	
	protected void setItemText(TextView tv, Week week){		
		String weekOfYear = "";
		mCalendar.setTimeInMillis(week.getStartDate() * 1000L);
		weekOfYear = String.valueOf(AbstractHomeActivity.getWeekOfYear(mCalendar));
		
		String dateText = week.getLongStartDate() + " - " + week.getLongEndDate();			
		String ausbildText = AbstractHomeActivity.sAusbildungsnachweis+week.getRealWeekNumber();
		String text = KALENDER_TEXT + weekOfYear +"\n" + dateText;
		int indexAusbild = text.length();
		text += ausbildText;
		int index = text.indexOf('\n');
		SpannableString spanString = new SpannableString(text);
		spanString.setSpan(new ForegroundColorSpan(mContext.getResources().getColor(R.color.colorDarkOrange)), 0, index, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
		spanString.setSpan(new AbsoluteSizeSpan(14), index, text.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
		spanString.setSpan(new ForegroundColorSpan(Color.BLACK), indexAusbild, text.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
		tv.setText(spanString);
			
	}

	@Override
	public void onClick(View v) {
		int id = v.getId();
		switch(id){
			case R.id.searchAusbildRenameView:
				final Long weekStartDate = (Long) v.getTag();
				int weekIndex = mWeeks.getWeekIndexByDate(weekStartDate);
				Week mWeek = mWeeks.getWeek(weekIndex);
				AlertDialog.Builder builderDialog = new AlertDialog.Builder(mContext);
				builderDialog.setTitle(R.string.dialogRenameTitle);
				View renameView = inflater.inflate(R.layout.dialog_rename, null);
				final EditText editView = (EditText) renameView.findViewById(R.id.dialogEditRenameView);
				if (mWeek != null && mWeek.getCustomWeekNumber() !=0){
					editView.setText(String.valueOf(mWeek.getCustomWeekNumber()));
				}
				builderDialog.setView(renameView);
				builderDialog.setPositiveButton(R.string.textRename, new DialogInterface.OnClickListener() {
					
					@Override
					public void onClick(DialogInterface dialog, int which) {
						String value = editView.getText().toString();
						String result = WorkServer.getInstance().changeAusbildNumber(weekStartDate, value);
						if (result != null && !WorkServer.ERROR_VALUE.equals(result)){
							int index = mWeeks.getWeekIndexByDate(weekStartDate);
							mWeeks.getWeek(index).setCustomWeekNumber(Integer.valueOf(value));
							SearchWeekTaskAdapter.this.notifyDataSetChanged();
							Toast.makeText(mContext, "Ausbild has renamed", Toast.LENGTH_SHORT).show();
						}
							
					}
				});
				builderDialog.setNegativeButton(R.string.cancelButton, new DialogInterface.OnClickListener() {
									
					@Override
					public void onClick(DialogInterface dialog, int which) {
						
					}
				});
				builderDialog.create().show();
				break;
		}
		
	}

}

